package com.pug.result;

import com.pug.result.enums.AdminResultInterface;
import com.pug.result.enums.AdminSuccessResultEnum;
import lombok.Data;


@Data
public class R {
    /**
     * 业务错误码
     */
    private Integer code;
    /**
     * 描述
     */
    private String msg;
    /**
     * 结果集
     */
    private Object data;

    // 私有化的目的：不是单列，是为了约束外界不允许通过实例化和set方法的方式进行复制调用返回
    // 全部统一使用静态方法 success和fail进行调用。从而达到统一的目的
    private R(Integer code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    public static R success(Object data) {
        return new R(AdminSuccessResultEnum.SUCCESS_RESULT.getCode(),
                AdminSuccessResultEnum.SUCCESS_RESULT.getMsg(), data);
    }

    public static R fail(AdminResultInterface adminResultEnum) {
        return new R(adminResultEnum.getCode(), adminResultEnum.getMsg(), null);
    }


    @Deprecated
    private static R fail(Integer code,String msg) {
        return new R(code, msg, null);
    }

    public R msg(String msg) {
        this.setMsg(msg);
        return this;
    }


    public R code(Integer code) {
        this.setCode(code);
        return this;
    }

}

